package com.kejiguan.ui.presenter;

import android.widget.Toast;

import com.android.volley.error.VolleyError;
import com.android.volley.response.Response;
import com.kejiguan.ui.model.ExhibitionNeItem;
import com.kejiguan.ui.model.NeItem;
import com.kejiguan.ui.request.GsonRequest;
import com.kejiguan.ui.request.RequestUtil;
import com.kejiguan.ui.response.ExhibitionModifyResponse;
import com.kejiguan.ui.response.ExhibitionQueryNeItemsResponse;
import com.kejiguan.ui.response.ExihibitionRemoveNeItemResponse;
import com.kejiguan.ui.response.NeItemManagerResponse;
import com.kejiguan.ui.util.CacheDataUtil;
import com.kejiguan.ui.util.CommonDate;
import com.kejiguan.ui.util.DateFormatUtil;

import org.greenrobot.eventbus.EventBus;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by YYY on 16/7/19.
 */
public class ModifyExhibitionPresenter extends BaseActivityPresenter {


    public void queryNeItemByWpaId(String wpaId) {

        long currentTime = System.currentTimeMillis();
        String currentDate = String.valueOf(currentTime);
        try {
            currentDate = DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String, String> params = new HashMap<>();
        params.put("requestCode", "705");
        params.put("messageId", currentDate + "S" + currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("operType", "queryList");
        params.put("wpaId", wpaId);


        final GsonRequest<ExhibitionQueryNeItemsResponse> request = new GsonRequest("QueryNeItemByWpaId", ExhibitionQueryNeItemsResponse.class, new Response.Listener<ExhibitionQueryNeItemsResponse>() {
            @Override
            public void onResponse(ExhibitionQueryNeItemsResponse response) {
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(mContext, "查询展项网元失败！", Toast.LENGTH_SHORT).show();

            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }

    public void removeNeItem(final NeItem neItem) {

        long currentTime = System.currentTimeMillis();
        String currentDate = String.valueOf(currentTime);
        try {
            currentDate = DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String, String> params = new HashMap<>();
        params.put("requestCode", "705");
        params.put("messageId", currentDate + "S" + currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("operType", "delete");

        params.put("id", neItem.getId());
        params.put("neName", neItem.getNeName());
        params.put("neType", neItem.getNeType());
        params.put("neState", "unusing");
        params.put("neIp", neItem.getNeIp());
        params.put("nePort", neItem.getNePort());
        params.put("wpaId", neItem.getWpaId());
        params.put("note", neItem.getNote());
        params.put("aliveTime", String.valueOf(neItem.getAliveTime()));
        params.put("mac", neItem.getMac());
        params.put("broadcastIp", neItem.getBroadcastIp());



        final GsonRequest<ExihibitionRemoveNeItemResponse> request = new GsonRequest("removeNeItem", ExihibitionRemoveNeItemResponse.class, new Response.Listener<ExihibitionRemoveNeItemResponse>() {
            @Override
            public void onResponse(ExihibitionRemoveNeItemResponse response) {
                response.setRemovedNeItem(neItem);
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(mContext, "查询展项网元失败！", Toast.LENGTH_SHORT).show();

            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }

    public void modifyExhibitionName(final ExhibitionNeItem exhibitionNeItem, final String exhibitionName)
    {

        long currentTime = System.currentTimeMillis();
        String currentDate = String.valueOf(currentTime);
        try {
            currentDate = DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String, String> params = new HashMap<>();
        params.put("requestCode", "714");
        params.put("messageId", currentDate + "S" + currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("operType", "mod");

        params.put("exhibitionId", exhibitionNeItem.getExhibitionId());
        params.put("exhibitionAreaId", exhibitionNeItem.getExhibitionAreaId());
        params.put("exhibitionName", exhibitionName);
        params.put("exhibitionState", exhibitionNeItem.getExhibitionState());
        params.put("exhibitionNote", exhibitionNeItem.getExhibitionNote());




        final GsonRequest<ExhibitionModifyResponse> request = new GsonRequest("ModifyExhibition", ExhibitionModifyResponse.class, new Response.Listener<ExhibitionModifyResponse>() {
            @Override
            public void onResponse(ExhibitionModifyResponse response) {
                exhibitionNeItem.setExhibitionName(exhibitionName);
                response.setModifiedExhibition(exhibitionNeItem);
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(mContext, "修改展项失败！", Toast.LENGTH_SHORT).show();

            }
        });
        RequestUtil.sendRequest(mContext, params, request);
    }

    public void modifyNeItem(final NeItem neItem)
    {

        long currentTime=System.currentTimeMillis();
        String currentDate=String.valueOf(currentTime);
        try {
            currentDate= DateFormatUtil.longToString(currentTime,"yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String,String> params=new HashMap<>();
        params.put("requestCode","705");
        params.put("messageId",currentDate+"S"+currentTime);
        params.put("systemCode",CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("operType","mod");

        params.put("neId",neItem.getId());
        params.put("wpaId",neItem.getWpaId());
        params.put("neName",neItem.getNeName());
        params.put("neIp",neItem.getNeIp());
        params.put("nePort",neItem.getNePort());
        params.put("neType",neItem.getNeType());
        params.put("aliveTime",String.valueOf(neItem.getAliveTime()));
        params.put("mac",neItem.getMac());
        params.put("broadcastIp",neItem.getBroadcastIp());
        params.put("neNote",neItem.getNote());


        final GsonRequest<NeItemManagerResponse> request = new GsonRequest("ModNeItem", NeItemManagerResponse.class, new Response.Listener<NeItemManagerResponse>() {
            @Override
            public void onResponse(NeItemManagerResponse response) {

                response.setNeItem(neItem);
                response.setOperType("mod");
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(mContext,"修改网元失败！",Toast.LENGTH_SHORT).show();
            }
        });

        RequestUtil.sendRequest(mContext, params, request);

    }

}
